package com.boe.csb.core.service.user;

import com.boe.csb.core.dao.UserOperateLogCriteria;
import com.boe.csb.core.dao.UserOperateLogMapper;
import com.boe.csb.core.entity.po.UserOperateLog;
import org.apache.ibatis.session.RowBounds;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * Created by tianxiang.luo on 16/12/9.
 */
@Service("adminUserService")
public class AdminUserService {


    @Autowired
    private UserOperateLogMapper userOperateLogMapper;

    public List<UserOperateLog> viewLog(Long userId,String type,int pageNum, int limit) {
        UserOperateLogCriteria criteria = new UserOperateLogCriteria();
        UserOperateLogCriteria.Criteria subCriteria = criteria.createCriteria();
        if(userId!=null){
            subCriteria.andUserIdEqualTo(userId);
        }
        if(type!=null){
            subCriteria.andLogTypeEqualTo(type);
        }
        criteria.setOrderByClause("id desc");
        return userOperateLogMapper.selectByExampleWithRowbounds(criteria, new RowBounds((pageNum - 1) * limit, limit));
    }

    public Long countLog(Long userId,String type) {
        UserOperateLogCriteria criteria = new UserOperateLogCriteria();
        UserOperateLogCriteria.Criteria subCriteria = criteria.createCriteria();
        if(userId!=null){
            subCriteria.andUserIdEqualTo(userId);
        }
        if(type!=null){
            subCriteria.andLogTypeEqualTo(type);
        }
        return userOperateLogMapper.countByExample(criteria);
    }
    
    public int deleteSession(String session){
    	int n = 0;
    	try{
    		n = userOperateLogMapper.deleteGetSession(session);
    		n =+ userOperateLogMapper.deleteGetSessionAttribute(session);
    	}catch(Exception e){
    		e.printStackTrace();
    	}
    	return n;
    }

}
